package com.midian.service.device.impl;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.midian.common.dao.BaseDaoTemplet;
import com.midian.common.dao.OrderHelper;
import com.midian.common.dao.SearchHelper;
import com.midian.common.dao.SearchHelper.Type;
import com.midian.common.exception.BizException;
import com.midian.common.pager.Pager;
import com.midian.model.device.Device;
import com.midian.service.common.BaseService;
import com.midian.service.device.IDeviceService;

/**
 * 设备号表业务实现类
 * @author Mengfh
 *
 */
@Service("deviceService")
public class DeviceServiceImpl extends BaseService implements IDeviceService {
	/**设备号表DAO*/
	private BaseDaoTemplet<Device> deviceDAO;
	
	@Resource(name = "deviceDAO")
	public void setDeviceDAO(BaseDaoTemplet<Device> deviceDAO) {
		this.deviceDAO = deviceDAO;
	}

	@Override
	public Device add(Device target) throws BizException {
		this.deviceDAO.add(target);
		return target;
	}

	@Override
	public void update(Device target) throws BizException {
		this.deviceDAO.updateByConvertHql(target);
	}

	@Override
	public void delete(Serializable id) throws BizException {
		this.deviceDAO.delete(id);
	}

	@Override
	public void delete(Serializable[] ids) throws BizException {
		this.deviceDAO.delete(ids);
	}

	@Override
	public Device get(Serializable id) throws BizException {
		return this.deviceDAO.get(id);
	}

	@Override
	public List<Device> showList(Device target, Pager pager, List<OrderHelper> helper) throws BizException {
		return this.deviceDAO.find(target, getSearchHelper(), helper, pager);
	}

	@Override
	public void opDelFlag(Serializable[] ids, Integer status) throws BizException {
		this.deviceDAO.opDelFlag(ids, status);
	}

	
	@Override
	public List<Device> getDeviceListByType(String type,Integer currentPage,Integer pageSize){
		String hql ="from Device o where o.frontType=? and o.isReceive = ? and o.deviceToken is not null order by o.id";
		return this.deviceDAO.find(hql, new Object[]{type,DeviceServiceImpl.ISRECEIVE_YES},currentPage,pageSize);
	}
	
	@Override
	protected List<SearchHelper> getSearchHelper() {
		String[] attrs = {};
		Type[] types = {};
		return SearchHelper.getSearchList(attrs, types);
	}

	/**
	 * 分类型列出所有isreceve状态为1的安卓或者苹果设备 
	 */
	@Override
	public List<Device> getDeviceListByIsReceive(Integer isReceive,String type) {
		String hql ="from Device o where o.isReceive = ? and o.frontType=?";
		return this.deviceDAO.find(hql, new Object[]{DeviceServiceImpl.ISRECEIVE_YES,type});
	}
}
